package com.mediatek.incallui.videocall;

import android.telecom.InCallService.VideoCall;
import android.telecom.VideoProfile;
import com.android.incallui.Call;
import com.android.incallui.Log;

import java.util.Objects;

/**
 * Track the video call state.
 * handle the call state related communication with VT Provider.
 */
public class VideoCallStateTracker {
    private static VideoCallStateTracker sInstance = new VideoCallStateTracker();
    private String mDisconnectCallId;

    private VideoCallStateTracker() {
    }

    public static VideoCallStateTracker getInstance() {
        return sInstance;
    }

    public void onDisconnect(Call call) {
        if (!Objects.equals(mDisconnectCallId, call.getId())) {
            mDisconnectCallId = call.getId();
            VideoCall videoCall = call.getVideoCall();
            // videoCall maybe null even videoState is bidirectional.
            if (VideoProfile.VideoState.isBidirectional(call.getVideoState()) && videoCall != null) {
                Log.d(this, "[onDisconnect]close VT manager for call: " + mDisconnectCallId);
                videoCall.onDisconnected();
                // FIXME: the FWK should guarantee the state not to be CLOSE
                videoCall.setVTClose();
            }
        }
    }
}
